package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
@Api(tags ="分类接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    // 分页查询分类列表
    @GetMapping("/page")
    @ApiOperation("查询分类列表")
    public Result<PageResult> getCategoryList(CategoryPageQueryDTO pageParams) {
        PageResult pageResult = categoryService.getCategoryList(pageParams);

        return Result.success(pageResult);
    }

    // 新增分类
    @PostMapping
    @ApiOperation("新增分类")
    public Result<Object> addCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.addCategory(categoryDTO);

        return Result.success();
    }

    // 启/禁用
    @ApiOperation("启/禁用分类")
    @PostMapping("/status/{status}")
    public Result<Object> changeCategoryStatus(@PathVariable Integer status, @RequestParam Integer id) {
        categoryService.changeStatus(status, id);

        return Result.success();
    }

    // 修改分类
    @ApiOperation("修改分类")
    @PutMapping
    public Result<Object> updateCategoryInfo(@RequestBody CategoryDTO categoryDTO) {
        categoryService.updateCategoryInfo(categoryDTO);

        return Result.success();
    }

    // 根据type查看分类
    @ApiOperation("根据type查询")
    @GetMapping("/list")
    public Result<List<Category>> selectByType(@RequestParam Integer type) {
        List<Category> category = categoryService.getCategoryByType(type);

        return Result.success(category);
    }

    // 根据id删除分类
    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result<String> deleteCategoryById(@RequestParam  Integer id) {
        categoryService.deleteCategoryById(id);

        return Result.success();
    }

}
